package com.poseidon.pokers.ui.main;

import android.databinding.Observable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.flyco.tablayout.listener.OnTabSelectListener;
import com.luojilab.componentservice.message.MessageService;
import com.luojilab.router.facade.annotation.RouteNode;
import com.poseidon.pokers.ui.base.MainModuleActivity;

import java.util.ArrayList;

import timber.log.Timber;

/**
 * Created by 42524 on 2018/1/26.
 */
@RouteNode(path = "/mainPage", desc = "首页")
public class MainActivity extends MainModuleActivity<MainActivityDelegate> implements OnTabSelectListener {


    private ArrayList<Fragment> fragmentArrayList;

    private Fragment messageFragment;

    private Fragment discoveryFragment;

    private Fragment recordFragment;

    private Fragment playFragment;

    private Fragment mineFragment;

    private Fragment mCurrentFragment;

    private FragmentTransaction ft;


    @Override
    public void actionViewModel(View view, Object o, int actionType) {

    }

    @Override
    protected void initData() {
        super.initData();
        fragmentArrayList=new ArrayList<>();
        MessageService messageService = (MessageService) router.getService(MessageService.class.getSimpleName());
        messageFragment = messageService.getMessageContainer();
        discoveryFragment=new Fragment();
        recordFragment=new Fragment();
        playFragment=new Fragment();
        mineFragment=new Fragment();

        fragmentArrayList.add(discoveryFragment);
        fragmentArrayList.add(messageFragment);
        fragmentArrayList.add(playFragment);
        fragmentArrayList.add(recordFragment);
        fragmentArrayList.add(mineFragment);

        viewDelegate.setTabSelectListener(this);
        changeTab(0);
        Timber.d("messageFragment:" + messageFragment);
    }


    private void changeTab(int position) {
        ft = getSupportFragmentManager().beginTransaction();

        //判断当前的Fragment是否为空，不为空则隐藏
        if (null != mCurrentFragment) {
            ft.hide(mCurrentFragment);
        }
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragmentArrayList.get(position).getClass().getName());

        if (null == fragment) {
            //如fragment为空，则之前未添加此Fragment。便从集合中取出
            fragment = fragmentArrayList.get(position);
        }
        mCurrentFragment = fragment;
        //判断此Fragment是否已经添加到FragmentTransaction事物中
        if (!fragment.isAdded()) {
            ft.add(viewDelegate.getContentMainId(), fragment, fragment.getClass().getName());
        } else {
            ft.show(fragment);
        }
        ft.commit();
    }

    @Override
    protected Class<MainActivityDelegate> getDelegateClass() {
        return MainActivityDelegate.class;
    }

    @Override
    public void onTabSelect(int position) {
        changeTab(position);
    }

    @Override
    public void onTabReselect(int position) {

    }
}
